{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "983bda06",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "d14ef297",
   "metadata": {},
   "source": [
    "## 上周回顾\n",
    "1.函数的定义  \n",
    "2.函数名的定义规则-->变量名一样的规则  \n",
    "3.调用函数的方式：func_name（参数）  \n",
    "4.参数：  \n",
    ">1.形式参数   \n",
    ">2.实际参数  \n",
    ">3.有多个参数时：  \n",
    ">>0.关键字参数  \n",
    ">>1.位置参数  \n",
    ">>2.默认参数  \n",
    "    \n",
    "## 本周知识  \n",
    "1.函数的返回值  \n",
    "2.函数的输入参数类型  \n",
    "3.函数的返回参数类型  \n",
    "4.模块的概念  \n",
    "5.如何创建一个模块，并且调用自己定义的模块  \n",
    "\n",
    "### 函数的返回值\n",
    ">1.返回值关键字：return  \n",
    ">>1.函数一定有返回值（无论是什么编程语言）  \n",
    ">>2.在其他编程语言中（如c语言，java等），返回值一定要写！不写会报错！；对于python（更高级的语言），返回值如果不写，给定默认值None  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9b091c6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践1\n",
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' ' + last_name\n",
    "    # print(full_name)\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7e3e50fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Li Hua'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_formatted_name(\"Li\",\"Hua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "93659f7e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Li Hua\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"Li\",\"Hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6cfb1822",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践2\n",
    "def log_in(username,password):\n",
    "    \"\"\"验证登录信息\"\"\"\n",
    "    if username == \"Lihua\" and password ==1234456:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ecf767e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"Lihua\",1234567)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cd9f5d6",
   "metadata": {},
   "source": [
    "### 函数的输入参数类型 \n",
    ">1.函数可以输入复杂的参数类型：\n",
    ">>1.函数参数为列表  \n",
    ">>2.函数参数为字典  \n",
    ">>3.…… \n",
    "\n",
    ">2.函数可以输入不确定个数的参数：  \n",
    ">>1. *args  \n",
    ">>2. ** kwargs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1037120e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_A(names):\n",
    "    \"\"\"测试函数参数输入为列表\"\"\"\n",
    "    return names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4c49c806",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Lihua', 'Alex', 'Ziqi']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_A([\"Lihua\",\"Alex\",\"Ziqi\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d33b395b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_B(person):\n",
    "    \"\"\"测试函数参数输入为字典\"\"\"\n",
    "    return person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4c38af55",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_id': 202101, 'age': 18, 'Job': 'student'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_B({\"stud_id\":202101,\"age\":18,\"Job\":\"student\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b781ecf7",
   "metadata": {},
   "source": [
    "> 2.函数可以输入不确定个数的参数：  \n",
    ">>1.args  \n",
    ">>>1.args可以接受多个数据（单值）  \n",
    ">>>2args将多个数据存在一个**元组**当中，实际还是一个变量  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "88a91877",
   "metadata": {},
   "outputs": [],
   "source": [
    "def food_ingred(food,*ingreds):\n",
    "    \"\"\"食物配料单\"\"\"\n",
    "    return food,ingreds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "00a983f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('pizza', ('mushroom', 'green peppers', 'extra cheese'))"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "food_ingred(\"pizza\",\"mushroom\",\"green peppers\",\"extra cheese\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "6dc9867c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def person(stud_name,**stud_course):\n",
    "    \"\"\"学生个人信息完善表\"\"\"\n",
    "    return stud_name,stud_course"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "03611eee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Lihua', {'course_星期一': 'API', 'course_星期二': 'app原型设计'})"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person(\"Lihua\",course_星期一 = \"API\",course_星期二= \"app原型设计\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfac230f",
   "metadata": {},
   "source": [
    "### 函数的输出（返回值）参数类型\n",
    ">1.在编程语言中，函数的返回值只能是一个值  \n",
    ">2.返回值可以是 字符串、数值、列表、字典等数据（单一值）  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "aa7b1c72",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    \"\"\"测试返回值return为字符串\"\"\"\n",
    "    return \"你好\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "754678fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "09722317",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值return为字符串\"\"\"\n",
    "    return 123456"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9660d456",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "123456"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f754a03c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值return为字符串\"\"\"\n",
    "    return [\"Lihua\",\"Alex\",\"Ziqi\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1b4b16e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Lihua', 'Alex', 'Ziqi']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "cfb229b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test4_return():\n",
    "    \"\"\"测试返回值return为字符串\"\"\"\n",
    "    return {\"stud_id\":202101,\"age\":18,\"Job\":\"student\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f461ac35",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_id': 202101, 'age': 18, 'Job': 'student'}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test4_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "0c837f23",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ******\n",
    "def test5_return():\n",
    "    \"\"\"测试返回值return为 多个值的情况\"\"\"\n",
    "    return 18,\"Lihua\",[\"Lihua\",\"Alex\",\"Ziqi\"],{\"stud_id\":202101,\"age\":18,\"Job\":\"student\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d7c599fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(18,\n",
       " 'Lihua',\n",
       " ['Lihua', 'Alex', 'Ziqi'],\n",
       " {'stud_id': 202101, 'age': 18, 'Job': 'student'})"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test5_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "746417e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test6_return():\n",
    "    \"\"\"测试返回值return为 多个值的情况\"\"\"\n",
    "    return 88,89,90,90"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "d971679d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(88, 89, 90, 90)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test6_return()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4d3f724",
   "metadata": {},
   "source": [
    "### 模块\n",
    ">1.使用模块可以隐藏函数的源代码  \n",
    ">2.使用的人不需要会底层代码（只需要会使用模块即可）  \n",
    ">3.模块的使用方法：  \n",
    ">>1.import模块名  \n",
    ">>> * 使用：模块名.函数名  \n",
    "\n",
    ">>2.from模块名 impor函数名\n",
    ">>> * 使用：函数名（）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cbbc2b07",
   "metadata": {},
   "outputs": [],
   "source": [
    "import study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a02094a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "study.search4vowels(\"hello,csh is surival\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "65d018f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8346ba34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=12, tm_mday=9, tm_hour=11, tm_min=49, tm_sec=32, tm_wday=3, tm_yday=343, tm_isdst=0)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c09ecb8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from time import localtime "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db92599b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
